<?php
/**
 * 菜单模型
 * @author sundongdong
 * @date 2016-07-05
 */
class System_Model_Menu extends Model_Base {
	/**
	 * 获取菜单列表
	 * @param number $is_show
	 */
	public function getMenu($is_show=1) {
		if ($is_show) {
			$where = "is_show=1";
		} else {
			$where = '1';
		}
		return $this->db()->fetchAll("SELECT * FROM `s_menu` WHERE $where ORDER BY menu_id desc");
	}
	/**
	 * 根据ID获取菜单信息
	 */
	public function getMenuById($menu_id) {
		return $this->db()->fetchRow("select * from s_menu where menu_id=:menu_id",array("menu_id"=>$menu_id));
	}
	/**
	 * 添加菜单
	 */
	public function replaceMenu($data) {
		try {
			$menu_id = $this->existMenu($data['name']);
			if ($menu_id && $menu_id != $data['menu_id']) {
				return array("opr" => 0, "content" => "该菜单名称已存在！");
			}
			if(!empty($data['menu_id'])) {
				$this->db("write_db")->update('s_menu', $data, 'menu_id='.$data['menu_id']);
			} else {
				$data['create_time'] = date("Y-m-d H:i:s");
				$this->db("write_db")->insert('s_menu', $data);
			}
			return array("opr"=>1,"content"=>"操作成功");
		} catch (Exception $e) {
			return array("opr"=>0, "content"=>$e->getMessage());
		}
	}
	/**
	 * 根据名字查看菜单是否存在
	 */
	public function existMenu($value) {
		return $this->db()->fetchOne("select menu_id from `s_menu` where name=:name", array('name'=>$value));
	}
	/**
	 * 根据父ID获取元素信息
	 */
	public function getNoteByPid($pid) {
		return $this->db()->fetchAll("select * from s_menu where fid = :fid and is_show='0' order by menu_id desc", array('fid' => $pid));
	}
	/**
	 * 删除菜单
	 */
	public function delMenu($menu_id) {
		try {
			$this->db('write_db')->delete('s_menu','menu_id='.$menu_id);
            return array("opr"=>1,"content"=>"success");
		} catch (Exception $e) {
			return array("opr"=>0,"content"=>$e->getMessage());
		}
	}
}